package it.irondev.appturismo.action;

import it.irondev.appturismo.entity.City;
import it.irondev.appturismo.entity.Event;
import it.irondev.appturismo.entity.Eventstype;
import it.irondev.appturismo.entity.Gallery;
import it.irondev.appturismo.entity.Language;
import it.irondev.appturismo.filter.BaseFilter;
import it.irondev.appturismo.service.ICityService;
import it.irondev.appturismo.service.IEventService;
import it.irondev.appturismo.service.IGalleryService;
import it.irondev.appturismo.service.ILanguageService;
import it.irondev.appturismo.util.ActionHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

@Controller("eventAction")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class EventAction extends AbstractAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7727109947109213094L;

	@Autowired
	IEventService eventService;
	
	@Autowired
	ILanguageService serviceLanguage;

	@Autowired
	ICityService serviceCity;
	
	@Autowired
	IGalleryService galleryService;

	private Integer idGallery;
	private Long idEvent;
	private List<Language> listLanguage;
	private List<Eventstype> listType;
	private List<City> listCity;
	private Event entityBean;
	private List<Event> events;
	private String startDate;
	private String endDate;
	
	
	private String stringaSearchLanguage;

	private static final SimpleDateFormat DATETIME_FORMAT = new SimpleDateFormat("dd-MM-yyyy hh:mm");

	
	public String list() {
		listLanguage = serviceLanguage.getAllLanguages();
		stringaSearchLanguage = ActionHelper.buildStringSearch(listLanguage); 
		return "listEvent";
	}

	public String listJson() {
		events = eventService.getAllEvents();
		return "listEventJson";
	}


	public String nuovo() {
		listType = eventService.getAllEventsType();
		listCity = serviceCity.getAllCity();
		listLanguage = serviceLanguage.getAllLanguages();
		return "newEvent";
	}

	public String edit() {
		listLanguage = serviceLanguage.getAllLanguages();
		listType = eventService.getAllEventsType();
		listCity = serviceCity.getAllCity();
		entityBean = eventService.getEventById(idEvent.intValue());
		return "editEvent";
	}

	public void delete() {
		if (getOper().equalsIgnoreCase("del")) {
			int idEntity = Integer.parseInt(getId());
			eventService.deleteEvent(idEntity);
		}
	}

	
	public String update() {
		try {
			entityBean.setEndDate(DATETIME_FORMAT.parse(endDate));
			entityBean.setStartDate(DATETIME_FORMAT.parse(startDate));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		eventService.updateEvent(entityBean);
		return "listEvent";
	}

	public String add() {
		try {
			entityBean.setEndDate(DATETIME_FORMAT.parse(endDate));
			entityBean.setStartDate(DATETIME_FORMAT.parse(startDate));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		eventService.addEvent(entityBean);
		return "listEvent";
	}
	
	//TODO:  rendere tranzasionale
	
	public String addGallery() {
		Gallery tmpGallery = new Gallery();
		galleryService.addGallery(tmpGallery);
		idGallery = tmpGallery.getIdgallery();
		entityBean = eventService.getEventById(idEvent.intValue());
		entityBean.setGallery(galleryService.getGalleryById(idGallery));
		eventService.updateEvent(entityBean);
		return "addEventGallery";
	}
	
	
	public String detailMobile() {
		entityBean = eventService.getEventById(idEvent.intValue());
		return "detailEventMobile";
	}
	
	public String listMobile() {
		BaseFilter filter = new BaseFilter();
		filter.setIdlanguage(serviceLanguage.getLanguageByPrefix(ActionContext.getContext().getLocale().getLanguage()).getIdlanguage());
		events = eventService.getEventsByFilter(filter);
		return "listEventMobile";
	}
	
	
	
	
	
	
	public Long getIdEvent() {
		return idEvent;
	}

	public void setIdEvent(Long idEvent) {
		this.idEvent = idEvent;
	}

	public List<Language> getListLanguage() {
		return listLanguage;
	}

	public void setListLanguage(List<Language> listLanguage) {
		this.listLanguage = listLanguage;
	}

	public List<Eventstype> getListType() {
		return listType;
	}

	public void setListType(List<Eventstype> listType) {
		this.listType = listType;
	}

	public List<City> getListCity() {
		return listCity;
	}

	public void setListCity(List<City> listCity) {
		this.listCity = listCity;
	}

	public Event getEntityBean() {
		return entityBean;
	}

	public void setEntityBean(Event entityBean) {
		this.entityBean = entityBean;
	}

	public List<Event> getEvents() {
		return events;
	}

	public void setEvents(List<Event> events) {
		this.events = events;
	}
	
	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}


	public String getStringaSearchLanguage() {
		return stringaSearchLanguage;
	}

	public void setStringaSearchLanguage(String stringaSearchLanguage) {
		this.stringaSearchLanguage = stringaSearchLanguage;
	}

	/**
	 * @return the idGallery
	 */
	public Integer getIdGallery() {
		return idGallery;
	}

	/**
	 * @param idGallery the idGallery to set
	 */
	public void setIdGallery(Integer idGallery) {
		this.idGallery = idGallery;
	}

	
}
